# -*- coding: utf-8 -*-

import StringIO, httplib, urllib2

class VerboseHTTPResponse(httplib.HTTPResponse):
    def _read_status(self):
        s = self.fp.read()
        print '-' * 20, 'Response', '-' * 20
        print s.split('\r\n\r\n')[0]
        self.fp = StringIO.StringIO(s)
        return httplib.HTTPResponse._read_status(self)
class VerboseHTTPConnection(httplib.HTTPConnection):
    response_class = VerboseHTTPResponse
    def send(self, s):
        print '-' * 20, 'Request', '-' * 20
        print s.strip()
        httplib.HTTPConnection.send(self, s)
class VerboseHTTPHandler(urllib2.HTTPHandler):
    def http_open(self, req):
        return self.do_open(VerboseHTTPConnection, req)

url = 'http://www.renren.com'

with open('../htmlfiles/h1.html','w') as f1:
    opener = urllib2.build_opener(VerboseHTTPHandler)
    page = opener.open(url)
    print >> f1, page.read()
    f1.close()

with open('../htmlfiles/h2.html','w') as f2:
    page = urllib2.urlopen(url)
    print >> f2, page.read()
    f2.close()

# request = urllib2.Request('http://localhost:8080')
# request.add_header('Accept-Encoding', 'deflate')
# request.add_header('User-Agent', 'Mozilla/5.0')
# inf = urllib2.urlopen(request)
